package niuke;

/**
 * description:
 * author:张腾
 * date:2021-06-29
 */

/**
 * 统计一个数字在升序数组中出现的次数
 */
public class NC74 {
    public int GetNumberOfK(int [] array , int k) {
        if(array.length==0) return 0;
        int left = 0;
        int right = array.length-1;
        //找第一个等于k的数
        while (left<right){
            int mid = (left+right)>>1;
            if (array[mid]>=k) right = mid;
            else left = mid+1;
        }
        int l = left;
        left=0;right=array.length-1;
        //找第一个大于k的数
        while (left<right){
            int mid = (left+right)>>1;
            if (array[mid]>k) right = mid;
            else left = mid+1;
        }
        if(array[left]==k) return left-l+1;
        else return left-l;
    }
}
